﻿using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinApp.Model;
using WinApp.Until;

namespace WinApp.ViewModel
{
    public class ExamListModel
    {
        public static PageDataRow ReadByMyExamination(SearchMyExaminationDto searchMyExaminationDto)
        {
            PageDataRow result = new PageDataRow();
            if (GlobalVariables.SystemSwitchModel == SwitchMode.OnLine)
                return WebApiHelper.PostWebAPI<PageDataRow>("api/Admin/SAAS_Enrollment/ReadByMyExamination", searchMyExaminationDto);
            else
            {
                using (var db = new SqlLiteDbContext())
                {
                    result.Total = db.Exams.Count();
                    List<MyExaminationOutPutDto> myCourseSessionEntityOutputDtos = new List<MyExaminationOutPutDto>();

                    var Query = db.Exams.AsQueryable();
                    if (searchMyExaminationDto.LastStatuArr.Count() > 0)
                    {
                        Query = Query.Where(x => x.LastStatu.HasValue && searchMyExaminationDto.LastStatuArr.Contains(x.LastStatu.Value));
                    }

                    myCourseSessionEntityOutputDtos = Query.Skip((searchMyExaminationDto.PageCondition.PageIndex - 1) * searchMyExaminationDto.PageCondition.PageSize).Take(searchMyExaminationDto.PageCondition.PageSize)
                    .Select(x => new MyExaminationOutPutDto
                    {
                        Id = x.Id,
                        BeginTime = x.dtBeginTime.Value,
                        EndTime = x.dtEndTime.HasValue ? x.dtEndTime.Value : null,
                        EnrollmentBeginTime = x.dtBeginTime.Value,
                        EnrollmentEndTime = x.dtEndTime.HasValue ? x.dtEndTime.Value : null,
                        ExaminationDuration = x.ExaminationDuration.HasValue ? x.ExaminationDuration.Value : 0,
                        UserId = x.UserId.HasValue ? x.UserId.Value : 0,
                        SurfacePlot = x.SurfacePlot != null ? x.SurfacePlot : "",
                        SAAS_ExaminationId = x.SAAS_ExaminationId.HasValue ? x.SAAS_ExaminationId.Value : 0,
                        LastSAAS_TestPaperRoundName = x.LastSAAS_TestPaperRoundName != null ? x.LastSAAS_TestPaperRoundName : "",
                        LastSAAS_TestPaperRoundId = x.LastSAAS_TestPaperRoundId.HasValue ? x.LastSAAS_TestPaperRoundId.Value : 0,
                        LastScore = x.LastScore.HasValue ? x.LastScore.Value : 0,
                        SAAS_ExaminationName = x.SAAS_ExaminationName != null ? x.SAAS_ExaminationName : "",
                        LastStatu = x.LastStatu.HasValue ? x.LastStatu.Value : 0,
                        SAAS_TestPaperId = x.SAAS_TestPaperId.HasValue ? x.SAAS_TestPaperId.Value : 0,
                        LastSAAS_ExaminationRoundId = x.LastSAAS_ExaminationRoundId.HasValue ? x.LastSAAS_ExaminationRoundId.Value : 0,
                        IsResume = x.IsResume.HasValue && x.IsResume == 1 ? true : false,
                        ExamType = x.ExamType.HasValue ? x.ExamType.Value : 0,
                        IsAppOfflineDownload = x.IsAppOfflineDownload.HasValue && x.IsAppOfflineDownload == 1 ? true : false,
                        IsCache = x.IsCache.HasValue && x.IsCache == 1 ? true : false,

                        //x.SurfacePlot != null ? x.SurfacePlot : "",

                    }).ToList();
                    result.Rows = JsonConvert.SerializeObject(myCourseSessionEntityOutputDtos);
                }
            }
            return result;
        }
    }
    public class TagExamsSessionDto
    {
        //public int ExamEnrollmentId { get; set; }
        //public int ExamTestPaperId { get; set; }
        //public int ExamExaminationRoundId { get; set; }
        //public int ExamTestPaperRoundId { get; set; }

        //public int? LastSAAS_ExaminationRoundId { get; set; }

        //public string StrStartTime { get; set; }
        //public string StrEndTime { get; set; }

        public int Id { get; set; }

        public int UserId { get; set; }

        public int SAAS_ExaminationId { get; set; }

        public string LastSAAS_TestPaperRoundName { get; set; }

        public int? LastSAAS_TestPaperRoundId { get; set; }

        public double? LastScore { get; set; }

        public int LastStatu { get; set; }

        public string SAAS_ExaminationName { get; set; }

        public string ExaminationDescription { get; set; }

        public DateTime? BeginTime { get; set; }
        public DateTime? EndTime { get; set; }

        public double? ExaminationDuration { get; set; } = 0.1;

        public int? SAAS_TestPaperId { get; set; } = 0;

        public int? LastSAAS_ExaminationRoundId { get; set; }
        public bool? IsResume { get; set; }

        public string SurfacePlot { get; set; }

    }
    public class SearchMyExaminationDto
    {

        public int UserId { get; set; }
        /// <summary>
        /// 学员名称
        /// </summary>
        public string ExaminationName { get; set; }

        public PageCondition PageCondition { get; set; }
        public int[] LastStatuArr { get; set; }
        public int? LastStatu { get; set; }

        public List<int> OrgIds { get; set; }

        /// <summary>
        /// 注册时间 开始 查询条件
        /// </summary>
        public DateTime? StartTime { get; set; }
        /// <summary>
        /// 注册时间 结束 查询条件
        /// </summary>
        public DateTime? EndTime { get; set; }

        public int? EnrollmentId { get; set; }

        public string LastStatuStr { get; set; }

    }

    public class MyExaminationOutPutDto
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public int SAAS_ExaminationId { get; set; }
        public string LastSAAS_TestPaperRoundName { get; set; }
        public int? LastSAAS_TestPaperRoundId { get; set; }
        public double? LastScore { get; set; }
        public string SAAS_ExaminationName { get; set; }
        public int LastStatu { get; set; }
        public string ExaminationDescription { get; set; }
        public DateTime BeginTime { get; set; }
        public DateTime? EndTime { get; set; }

        public int? ExamType { get; set; }

        public bool IsCache { get; set; }

        public DateTime? CreatedTime { get; set; }
        public double ExaminationDuration { get; set; }
        public int SAAS_TestPaperId { get; set; }
        public int? LastSAAS_ExaminationRoundId { get; set; }

        //public int ExaminationType { get; set; }
        public bool IsResume { get; set; }
        public string CurriculumName { get; set; }
        /// <summary>
        /// 试卷的及格分
        /// </summary>
        public int TestPaperPassScore { get; set; }
        public string SurfacePlot { get; set; }
        //注册时间
        public DateTime? EnrollmentsTime { get; set; }
        public DateTime? LastUpdatedTime { get; set; }

        [DisplayName("轮次的试卷内容json格式")]
        public string RoundTestPaperJSON { get; set; }
        public List<StudentQuestionResultOutputDto> StudentQuestionResults { get; set; }
        public bool IsAppOfflineDownload { get; set; }

        public DateTime EnrollmentBeginTime { get; set; }
        public DateTime? EnrollmentEndTime { get; set; }
        public DateTime ExamBeginTime { get; set; }
        public DateTime? ExamEndTime { get; set; }

        #region 自我注册相关字段 
        public int? Capacity { get; set; }
        public int? ReservedSeats { get; set; }
        public string ReservedNames { get; set; }
        #endregion
    }

    public class StudentQuestionResultOutputDto
    {
        /// <summary>
        /// 获取或设置 编号
        /// </summary>
        [DisplayName("ID")]
        public int Id { get; set; }

        /// <summary>
        /// 获取或设置 Except for simple questions, the field length is very small
        /// </summary>
        [DisplayName("Except for simple questions, the field length is very small")]
        public string LastStudentAnswer { get; set; }

        /// <summary>
        /// 获取或设置 The time to submit the test questions, using the server time zone
        /// </summary>
        [DisplayName("The time to submit the test questions, using the server time zone")]
        public DateTime LastAnswerTime { get; set; }
        [DisplayName("Locked or not")]
        public bool IsLocked { get; set; }

        /// <summary>
        /// 获取或设置 Delete time
        /// </summary>
        [DisplayName("Deletion time")]
        public DateTime? DeletedTime { get; set; }
        /// <summary>
        /// 获取或设置 residence time
        /// </summary>
        [DisplayName("residence time")]
        public double TotalStayTime { get; set; }

        ///// <summary>
        ///// 获取或设置 Device record for answering, such as Chrome browser
        ///// </summary>
        //[DisplayName("Device record for answering, such as Chrome browser")]
        //public string LastUserAgent { get; set; }

        public bool IsFlag { get; set; }
        public int AnswerCount { get; set; }

        [Required, StringLength(4000)]
        public string TraceAnswer { get; set; }

        [Required, StringLength(4000)]
        public string TraceAnswerTime { get; set; }

        [Required, StringLength(1000)]
        public string TraceStayTime { get; set; }

        [Required, StringLength(1000)]
        public string TraceIsCorrect { get; set; }

        /// <summary>
        /// 获取或设置 Is it a red line question
        /// </summary>
        [DisplayName("Is it a red line question")]
        public bool IsRedQuestion { get; set; }

        /// <summary>
        /// 获取或设置 试题Id
        /// </summary>
        [DisplayName("Question ID")]
        public int SAAS_QuestionId { get; set; }
        [DisplayName("Test question version Id")]
        public int SAAS_QuestionVersionId { get; set; }

        /// <summary>
        /// 获取或设置 Student Id
        /// </summary>
        [DisplayName("Student Id")]
        public int UserId { get; set; }

        /// <summary>
        /// 获取或设置 Whether the answer is correct
        /// </summary>
        [DisplayName("Whether the answer is correct")]
        public bool? LastIsCorrect { get; set; }

        /// <summary>
        /// 获取或设置 Score of the test question
        /// </summary>
        [DisplayName("Score of the test question")]
        public double? LastScore { get; set; }

        /// <summary>
        /// 获取或设置 Test round Id
        /// </summary>
        [DisplayName("Test round Id")]
        public int SAAS_ExaminationRoundId { get; set; }

        /// <summary>
        /// 获取或设置 Creator Id
        /// </summary>
        [DisplayName("Creator")]
        public int? CreatorId { get; set; }

        /// <summary>
        /// 获取或设置 Created time
        /// </summary>
        [DisplayName("Creation time")]
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 获取或设置 Updater Id
        /// </summary>
        [DisplayName("Updater")]
        public int? LastUpdaterId { get; set; }

        /// <summary>
        /// 获取或设置 Updated time
        /// </summary>
        [DisplayName("Update time")]
        public DateTime? LastUpdatedTime { get; set; }
        /// <summary>
        /// Whether to score，目前只有人工阅卷题用到
        /// </summary>
        [DisplayName("Whether to score")]
        public bool? IsGrade { get; set; }


        [DisplayName("The last residence time to answer this question")]
        public double LastStayTime { get; set; }
        /// <summary>
        /// Has it been calculated
        /// </summary>
        [DisplayName("Has it been calculated")]
        public bool? IsCalc { get; set; }
        public int? ExaminationId { get; set; }
        public int? TestPaperId { get; set; }
    }
}
